/**
 * 
 */
package cn.jhz.filesharingsystem.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author asus
 *
 */
public class CaptchaUtil {
	/* 验证码img宽 */
	private int width;
	/* 验证码img高 */
	private int height;
	/* 验证码字符个数 */
	private int num;
	/* 验证码的字典 */
	private String code;
	/* 取随机数的对象*/	
	static private final Random ran = new Random();
	
	
	private CaptchaUtil() {
		code = "0123456789";
		num = 4;
	}
	
	static private class Captcha{
		static private final CaptchaUtil captchaUtil = new CaptchaUtil();
	}
	
	static public CaptchaUtil getInstance() {
		return Captcha.captchaUtil;
	}

	public void set(int width, int height, int num, String code) {
		this.width = width;
		this.height = height;
		this.setNum(num);
		this.setCode(code);
	}
	public void set(int width, int height) {
		this.width = width;
		this.height = height;
	}
	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * @return the num
	 */
	public int getNum() {
		return num;
	}

	/**
	 * @param num the num to set
	 */
	public void setNum(int num) {
		this.num = num;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/* 生成随机码 */
	public String generateCheckcode() {
		StringBuffer cc = new StringBuffer();
		for(int i=0; i<num; i++) {
			cc.append(code.charAt(ran.nextInt(code.length())));
		}
		return cc.toString();
	}
	
	public BufferedImage generateCheckImg(String checkcode) {
		/* 创建一个图片对象 */
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		/* 获取图片对象的画笔 */
		Graphics2D graphic = img.createGraphics();
		graphic.setColor(Color.WHITE);
		graphic.fillRect(0, 0, width, height);
		graphic.setColor(Color.BLACK);
		graphic.drawRect(0, 0, width-1, height-1);
		Font font = new Font("宋体", Font.BOLD+Font.ITALIC, (int)(height*0.8));
		graphic.setFont(font);
		/* 写入验证码 */
		for(int i=0; i<num; i++) {
			graphic.setColor(new Color(ran.nextInt(155), ran.nextInt(255), ran.nextInt(255)));
			graphic.drawString(String.valueOf(checkcode.charAt(i)), i*(width/num)+4, (int)(height*0.8));
		}
		/* 加一些点 */
		for(int i=0; i<(width+height); i++) {
			graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			graphic.drawOval(ran.nextInt(width), ran.nextInt(height), 1, 1);
		}
		/* 加一些线 */
		for(int i=0; i<2; i++) {
			graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			graphic.drawLine(0, ran.nextInt(height), width, ran.nextInt(height));
		}
		return img;
	}
	
	
}
